class DDLItem {
  final String pid;  // 新增：唯一标识符
  final String title;
  final DateTime? deadline;
  DateTime? startDate;  // 可选的开始日期
  String? description;  // 可选的详细描述
  List<AttachmentItem> attachments;
  bool isUrgent;  // 改为可变
  bool isCompleted;
  bool isAbandoned;  // 新增：是否放弃

  DDLItem({
    String? pid,  // 可选参数，如果不提供则自动生成
    required this.title,
    this.deadline,
    this.startDate,
    this.description,
    List<AttachmentItem>? attachments,
    this.isUrgent = false,
    this.isCompleted = false,
    this.isAbandoned = false,  // 默认不放弃
  }) : pid = pid ?? DateTime.now().millisecondsSinceEpoch.toString(),  // 使用时间戳作为默认pid
       attachments = attachments ?? [];

  // 从JSON创建对象
  factory DDLItem.fromJson(Map<String, dynamic> json) {
    return DDLItem(
      pid: json['pid'] as String? ?? DateTime.now().millisecondsSinceEpoch.toString(),
      title: json['title'] as String,
      deadline: json['deadline'] != null ? DateTime.parse(json['deadline']) : null,
      startDate: json['startDate'] != null ? DateTime.parse(json['startDate']) : null,
      description: json['description'] as String?,
      attachments: (json['attachments'] as List<dynamic>?)
          ?.map((e) => AttachmentItem.fromJson(e as Map<String, dynamic>))
          .toList() ?? [],
      isUrgent: json['isUrgent'] as bool? ?? false,
      isCompleted: json['isCompleted'] as bool? ?? false,
      isAbandoned: json['isAbandoned'] as bool? ?? false,  // 新增
    );
  }

  // 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'pid': pid,  // 新增
      'title': title,
      'deadline': deadline?.toIso8601String(),
      'startDate': startDate?.toIso8601String(),
      'description': description,
      'attachments': attachments.map((e) => e.toJson()).toList(),
      'isUrgent': isUrgent,
      'isCompleted': isCompleted,
      'isAbandoned': isAbandoned,  // 新增
    };
  }

  // 用于创建副本的方法，在编辑时使用
  DDLItem copyWith({
    String? pid,  // 新增
    String? title,
    DateTime? deadline,
    DateTime? startDate,
    String? description,
    List<AttachmentItem>? attachments,
    bool? isUrgent,
    bool? isCompleted,
    bool? isAbandoned,  // 新增
  }) {
    return DDLItem(
      pid: pid ?? this.pid,  // 新增
      title: title ?? this.title,
      deadline: deadline ?? this.deadline,
      startDate: startDate ?? this.startDate,
      description: description ?? this.description,
      attachments: attachments ?? List.from(this.attachments),
      isUrgent: isUrgent ?? this.isUrgent,
      isCompleted: isCompleted ?? this.isCompleted,
      isAbandoned: isAbandoned ?? this.isAbandoned,  // 新增
    );
  }
}

// Add this class before or after DDLItem class
class AttachmentItem {
  final String name;
  final String path;
  final AttachmentType type;

  AttachmentItem({
    required this.name,
    required this.path,
    required this.type,
  });

  // 从JSON创建对象
  factory AttachmentItem.fromJson(Map<String, dynamic> json) {
    return AttachmentItem(
      name: json['name'] as String,
      path: json['path'] as String,
      type: AttachmentType.values.firstWhere(
        (e) => e.toString() == 'AttachmentType.${json['type']}',
        orElse: () => AttachmentType.file,
      ),
    );
  }

  // 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'path': path,
      'type': type.toString().split('.').last,
    };
  }
}

enum AttachmentType { file, image } 